<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>边带权并查集 学习笔记 &amp;amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="非常巧妙">
<meta property="og:type" content="article">
<meta property="og:title" content="边带权并查集 学习笔记 &amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解">
<meta property="og:url" content="http://example.com/2022/01/15/%E8%BE%B9%E5%B8%A6%E6%9D%83%E5%B9%B6%E6%9F%A5%E9%9B%86%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20&amp;%20%E6%B4%9B%E8%B0%B7P1196%20%E3%80%8CNOI2002%E3%80%8D%20%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4%20%E9%A2%98%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="非常巧妙">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s2.loli.net/2022/01/15/CEX71MxdZHtkrzi.png">
<meta property="og:image" content="https://s2.loli.net/2022/01/15/irMABcT9nEsVN7R.png">
<meta property="og:image" content="https://s2.loli.net/2022/01/15/pi86GsudzWOonUw.png">
<meta property="article:published_time" content="2022-01-15T09:37:04.613Z">
<meta property="article:modified_time" content="2023-07-19T11:50:08.597Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2022/01/15/CEX71MxdZHtkrzi.png">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-边带权并查集 学习笔记 &amp;amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2022/01/15/%E8%BE%B9%E5%B8%A6%E6%9D%83%E5%B9%B6%E6%9F%A5%E9%9B%86%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20&amp;%20%E6%B4%9B%E8%B0%B7P1196%20%E3%80%8CNOI2002%E3%80%8D%20%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4%20%E9%A2%98%E8%A7%A3/" class="article-date">
  <time class="dt-published" datetime="2022-01-15T09:37:04.613Z" itemprop="datePublished">2022-01-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      边带权并查集 学习笔记 &amp;amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>非常巧妙</p>
<p>花了2h总算把边带权并查集整明白了qaq</p>
<h1 id="1-边带权并查集的用途"><a href="#1-边带权并查集的用途" class="headerlink" title="1.边带权并查集的用途"></a>1.边带权并查集的用途</h1><p>众所周知，并查集擅长维护与可传递关系有关的信息。然而我们有时会发现并查集所维护的信息不够用，这时“边带权并查集”就应运而生了。</p>
<h1 id="2-例题与思路"><a href="#2-例题与思路" class="headerlink" title="2.例题与思路"></a>2.例题与思路</h1><p>这里通过例题 <a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1196">洛谷P1196 [NOI2002] 银河英雄传说</a> 来介绍边带权并查集的思想。题面请点击链接查看。</p>
<h2 id="2-1-暴力"><a href="#2-1-暴力" class="headerlink" title="2.1.暴力"></a>2.1.暴力</h2><p>拿到这道题我的第一想法就是用链表模拟。对于两艘在同一列的战舰，只需知道它们到队首的距离（设距离分别为 $dis_1$ 和 $dis_2$）就可以知道它们之间的距离（为 $|dis_1-dis_2|+1$）。对于每艘战舰，记录它前面的战舰是哪一艘，查询时通过暴力往前跳来查询 $dis_1$ 和 $dis_2$，合并时暴力合并。这样显然会超时，于是我们考虑优化。</p>
<h2 id="2-2-优化"><a href="#2-2-优化" class="headerlink" title="2.2.优化"></a>2.2.优化</h2><p>可以考虑使用路径压缩的方式进行优化。对于这样一个链表:</p>
<p><img src="https://s2.loli.net/2022/01/15/CEX71MxdZHtkrzi.png" alt="屏幕截图 2022-01-15 171539.png"></p>
<p>它等价于：</p>
<p><img src="https://s2.loli.net/2022/01/15/irMABcT9nEsVN7R.png" alt="屏幕截图 2022-01-15 171810.png"></p>
<p>同时这样查询时要跳的步数就少很多。这其实就是路径压缩。</p>
<p>同时我们还需要记录每一列的战舰数，在合并时只需要：</p>
<p><img src="https://s2.loli.net/2022/01/15/pi86GsudzWOonUw.png" alt="屏幕截图 2022-01-15 172849.png"></p>
<p>就可以了。</p>
<p>单次操作时间复杂度和并查集一模一样，是 $\Theta(\alpha(n))$ 的，非常高效。</p>
<h2 id="2-3-Code"><a href="#2-3-Code" class="headerlink" title="2.3.Code"></a>2.3.Code</h2><p>废话少说，放码过来！</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 30000</span></span><br><span class="line"><span class="type">int</span> t,fa[MAXN+<span class="number">5</span>],dis[MAXN+<span class="number">5</span>]<span class="comment">/*到父亲战舰之间隔了多少战舰*/</span>,siz[MAXN+<span class="number">5</span>]<span class="comment">/*每一列的战舰数*/</span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">get</span><span class="params">(<span class="type">int</span> x)</span></span>&#123;<span class="comment">//查询</span></span><br><span class="line">    <span class="keyword">if</span>(fa[x]==x)&#123;</span><br><span class="line">        <span class="keyword">return</span> x;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="type">int</span> res=<span class="built_in">get</span>(fa[x]);</span><br><span class="line">        dis[x]+=dis[fa[x]];<span class="comment">//路径压缩</span></span><br><span class="line">        fa[x]=res;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    ios::<span class="built_in">sync_with_stdio</span>(<span class="literal">false</span>);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=MAXN;i++)&#123;</span><br><span class="line">        fa[i]=i;</span><br><span class="line">        siz[i]=<span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cin&gt;&gt;t;</span><br><span class="line">    <span class="keyword">while</span>(t--)&#123;</span><br><span class="line">        string op;cin&gt;&gt;op;</span><br><span class="line">        <span class="keyword">if</span>(op==<span class="string">&quot;M&quot;</span>)&#123;<span class="comment">//合并</span></span><br><span class="line">            <span class="type">int</span> i,j;cin&gt;&gt;i&gt;&gt;j;</span><br><span class="line">            i=<span class="built_in">get</span>(i);j=<span class="built_in">get</span>(j);</span><br><span class="line">            dis[i]+=siz[j];</span><br><span class="line">            fa[i]=j;</span><br><span class="line">            siz[j]+=siz[i];</span><br><span class="line">            siz[i]=<span class="number">0</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="type">int</span> i,j;cin&gt;&gt;i&gt;&gt;j;</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">get</span>(i)!=<span class="built_in">get</span>(j))&#123;</span><br><span class="line">                cout&lt;&lt;<span class="number">-1</span>&lt;&lt;endl;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                cout&lt;&lt;<span class="built_in">abs</span>(dis[i]-dis[j])<span class="number">-1</span>&lt;&lt;endl;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>据说还有一个扩展域并查集，回头博主再了解一下，<del>现在博主要去恰饭了</del></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/01/15/%E8%BE%B9%E5%B8%A6%E6%9D%83%E5%B9%B6%E6%9F%A5%E9%9B%86%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20&amp;%20%E6%B4%9B%E8%B0%B7P1196%20%E3%80%8CNOI2002%E3%80%8D%20%E9%93%B6%E6%B2%B3%E8%8B%B1%E9%9B%84%E4%BC%A0%E8%AF%B4%20%E9%A2%98%E8%A7%A3/" data-id="clk9o8ln9000k8wi50ifi4a94" data-title="边带权并查集 学习笔记 &amp;amp; 洛谷P1196 「NOI2002」 银河英雄传说 题解" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/01/26/%E5%B9%B3%E8%A1%A1%E6%A0%91%20Treap%EF%BC%88%E6%A0%91%E5%A0%86%EF%BC%89%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          平衡树 Treap（树堆） 学习笔记
        
      </div>
    </a>
  
  
    <a href="/2021/08/11/%E7%82%B9%E5%88%86%E6%B2%BB%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">点分治 学习笔记</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>